// HelloClient.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	HRESULT hr;
	hr = ::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
	if(hr != S_OK){
		cout << "Initialize Fail"<< endl;
		return 0;
	}

	CLSID clsid;

	hr = ::CLSIDFromProgID(L"HelloServer.Hello.1", &clsid);
	if(hr!=S_OK){
		cout << "Don't exist in registry" << endl;
		return 0;
	}

	IUnknown* pUnk = NULL;

	hr = ::CoCreateInstance(clsid, NULL, CLSCTX_ALL, IID_IUnknown, (void**)&pUnk);

	if(hr != S_OK){
		cout << "Don't create instance" << endl;
		return 0;
	}

	IHello* pHello = NULL;

	hr = pUnk->QueryInterface(IID_IHello, (void**)&pHello);
	if(hr != S_OK){
		cout << "IHello interface fail" << endl;
		return 0;
	}

	pUnk->Release();

	wchar_t* message = NULL;
	wchar_t* name;
	char buffer[256];
	cout << "insert name :";
	cin >> buffer;
	name = (wchar_t*)buffer;

	hr = pHello->sayHello(name, &message);
	if(hr!=S_OK){
		cout << "Don't call method" << endl;
		return 0;
	}

	cout << (char*) message << endl;
	::CoTaskMemFree((LPVOID)message);

	pHello->Release();

	::CoUninitialize();
	return 0;
}

